<?php
include('../../../config.php');
include(PUNG_ROOT . '/admin/header.php');

$adminmenu = array(
MENU_001 => 'index.php'
);

if(isset($_POST['create']))
{
	$db -> db_query("INSERT INTO " . DB_PREFIX . "colmenu_menu (name, link, parent, weight) VALUES (
	'" . addslashes($_POST['menuname']) . "',
	'" . $_POST['mainid'] . "',
	'" . $_POST['parentid'] . "',
	'0'
	)");
	
	redirect_with_message('msg', 'MENU_009');
}

if(isset($_POST['update']))
{
	$sql = $db -> db_query("SELECT * FROM " . DB_PREFIX . "colmenu_menu ORDER BY id ASC");
	$sql = $db -> db_result();
	
	foreach($sql as $item)
	{
		$db -> db_query("UPDATE " . DB_PREFIX . "colmenu_menu SET 
		name = '" . $_POST['itemname' . $item['id']] . "',
		weight = '" . $_POST['itemweight' . $item['id']] . "'
		WHERE id = '" . $item['id'] . "'");
	}
	
	redirect_with_message('msg', 'MENU_015');
}

if(isset($_POST['updatesettings']))
{
	$db -> db_query("UPDATE " . DB_PREFIX . "blocks SET title = '" . addslashes($_POST['menutitle']) . "' WHERE owner = 'collapsing_menu'");
	
	redirect_with_message('msg', 'MENU_015');
}



if(isset($_GET['delete']))
{
	$id = $_GET['delete'];
	$db -> db_query("DELETE FROM " . DB_PREFIX . "colmenu_menu WHERE id = '$id'");
	$db -> db_query("DELETE FROM " . DB_PREFIX . "colmenu_menu WHERE parent = '$id'");
	
	redirect_with_message('msg', 'MENU_016');
}

include(PUNG_ROOT . '/admin/head.php');

$sql = $db -> db_query("SELECT * FROM " . DB_PREFIX . "colmenu_menu WHERE parent = '0' ORDER BY weight ASC");
$sql = $db -> db_result();

$sql2 = $db -> db_query("SELECT title FROM " . DB_PREFIX . "blocks WHERE owner = 'collapsing_menu'");
$sql2 = $db -> db_result();

?>

<h3><?php print MENU_017; ?></h3>
<form action="" method="post">
<table width="100%">
<tr>
	<td><strong><?php print MENU_018; ?>:</strong></td>
	<td><input type="text" style="width: 250px" name="menutitle" value="<?php print $sql2[0]['title']; ?>"/></td>
</tr>
</table>
<br />
<input type="submit" name="updatesettings" class="form_button" value="<?php print MENU_011; ?>" />
</form>


<h3><?php print MENU_002; ?></h3>

<form action="" method="post">

<table width="100%">
<tr>
	<th width="20">&nbsp;</th>
	<th><?php print MENU_004; ?></th>
	<th width="40"><?php print MENU_010; ?></th>
	<th width="40">&nbsp;</th>
</tr>
<?php foreach($sql as $item) { ?>
<tr>
	<td><img src="<?php print PUNG_URL . '/images/admin_img/display.png'; ?>" alt="" /></td>
	<td><input type="text" value="<?php print $item['name']; ?>" style="width:250px" name="itemname<?php print $item['id']; ?>" /></td>
	<td align="center"><input type="text" style="width: 20px" value="<?php print $item['weight']; ?>" name="itemweight<?php print $item['id']; ?>" /></td>
	<td align="center">
	<a href="#<?php print $item['id']; ?>" onclick="confirm_box('<?php print MENU_013; ?>', '?delete=<?php print $item['id']; ?>');" 
	title="<?php print MENU_014; ?>"><img src="<?php print PUNG_URL; ?>/images/admin_img/uninstall.png" alt="<?php print MENU_014; ?>" /></a>
	</td>
</tr>

<?php
// Submenus
$submenu = $db -> db_query("SELECT * FROM " . DB_PREFIX . "colmenu_menu WHERE parent = '" . $item['id'] . "' ORDER BY weight ASC");
$submenu = $db -> db_result();

foreach($submenu as $subitem)
{
	print '<tr>';
	print '<td align="center"><img src="' . PUNG_URL . '/images/admin_img/arrow.png" alt="" /></td>';
	print '<td><input type="text" value="' . $subitem['name'] . '" style="width:250px" name="itemname' . $subitem['id'] . '" /></td>';
	print '<td align="center"><input type="text" style="width: 20px" value="' . $subitem['weight'] . '" name="itemweight' . $subitem['id'] . '" /></td>';
	print '<td align="center">';
	?>
	<a href="#<?php print $subitem['id']; ?>" onclick="confirm_box('<?php print MENU_013; ?>', '?delete=<?php print $subitem['id']; ?>');" 
	title="<?php print MENU_014; ?>"><img src="<?php print PUNG_URL; ?>/images/admin_img/uninstall.png" alt="<?php print MENU_014; ?>" /></a>
	<?php
	print '</td>';
	print '</tr>';
}
?>

<?php } ?>
</table>
<br />
<input type="submit" name="update" class="form_button" value="<?php print MENU_011; ?>" />
</form>

<h3><?php print MENU_003; ?></h3>

<form action="" method="post">
<table width="100%">
<tr>
	<td><strong><?php print MENU_006; ?>:</strong></td>
	<td>
	<select name="parentid" style="width: 250px">
	<option value="0"><?php print MENU_007; ?></option>
	<?php foreach($sql as $title) { ?>
	<option value="<?php print $title['id']; ?>"><?php print stripslashes($title['name']); ?></option>
	<?php } ?>
	</select>
	</td>
</tr>
<tr>
	<td><strong><?php print MENU_004; ?>:</strong></td>
	<td><input type="text" style="width: 250px" name="menuname" /></td>
</tr>
<?php
$sql = $db -> db_query("SELECT id, title FROM " . DB_PREFIX . "content ORDER BY id ASC");
$sql = $db -> db_result();
?>
<tr>
	<td><strong><?php print MENU_005; ?>:</strong></td>
	<td>
	<select name="mainid" style="width: 250px">
    <option value="/?"><?php print MENU_020;?></option>
	<?php foreach($sql as $title) { ?>
	<option value="/?m=content&id=<?php print $title['id']; ?>"><?php print stripslashes($title['title']); ?></option>
	<?php } ?>
	</select>
	</td>
</tr>
<?php
$sql = $db -> db_query("SELECT * FROM " . DB_PREFIX . "colmenu_menu WHERE parent = '0' ORDER BY id ASC");
$sql = $db -> db_result();
?>
</table>
<br />
<input type="submit" name="create" class="form_button" value="<?php print MENU_008; ?>" />
</form>

<?php
include(PUNG_ROOT . '/admin/footer.php');
?>